<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        async function add() {
            let obj = {
                bookname: '书1',
                author: 'jhy',
                publisher: '123'
            }
            let result = await fetch('http://ajax-base-api-t.itheima.net/api/addbook', {
                method: 'post',
                headers: {
                    'Content-type': 'application/json'
                },
                // 原生方法，需要通过JSON.stringify将对象转为json字符串

                body: JSON.stringify(obj)
            })
            let json = await result.json()
            console.log(json);

        }
        add()
        //另一个headers写法
        async function add1() {
            let obj = {
                bookname: '书1',
                author: 'jhy',
                publisher: '123'
            }
            // 实例化headers对象
            const headers = new Headers()
            // append方法添加请求头
            headers.append('Content-Type', 'application/json')
            let result = await fetch('http://ajax-base-api-t.itheima.net/api/addbook', {
                method: 'post',
                headers,
                // 原生方法，需要通过JSON.stringify将对象转为json字符串
                body: JSON.stringify(obj)
            })
            let json = await result.json()
            console.log(json);
        }
        add1()
    </script>
</body>

</html>